//页面初始化
document.ready = function(callback) {
    if (document.addEventListener) {
        document.addEventListener('DOMContentLoaded', function() {
            document.removeEventListener('DOMContentLoaded', arguments.callee, false);
            callback();
        }, false);
    }
    else if (document.attachEvent) {
        document.attachEvent('onreadystatechange', function() {
            if (document.readyState == 'complete') {
                document.detachEvent('onreadystatechange', arguments.callee);
                callback();
            }
        });
    }
    else if (document.lastChild == document.body) {
        callback();
    }
}



//根据id操作DOM元素
function getItem(id) {
    return document.getElementById(id);
}

function getValue(id) {
    return getItem(id).value;
}

function getAjaxValue(id) {
    return encodeURIComponent(getValue(id));
}

function setValue(id, value) {
    getItem(id).value = value;
}

function getSelectText(id) {
    return getItem(id).options[getItem(id).selectedIndex].text;
}

function setSelectItem(id, list) {
    var objSelect = getItem(id);
    for (var i = 0; i < list.length; i++) {
        var option = document.createElement('option');

        option.text = list[i].text;
        option.value = list[i].value;
        objSelect.options.add(option);
    }
}

function setClass(id, className) {
    getItem(id).className = className;
}

function getClass(id) {
    return getItem(id).className;
}

function getHtml(id) {
    return getItem(id).innerHTML;
}

function setHtml(id, value) {
    getItem(id).innerHTML = value;
}

function setDisplay(id, status) {
    getItem(id).style.display = status;
}

function locationSelect(id, value) {
    for (var i = 0; i < getItem(id).options.length; i++) {
        if (getItem(id).options[i].value == value) {
            getItem(id).options[i].selected = true;
            break;
        }
    }
}



//根据name操作DOM元素
function getItemByName(name, index = 0) {
    return document.getElementsByName(name)[index];
}

function getValueByName(name, index = 0) {
    return getItemByName(name, index).value;
}

function getAjaxValueByName(name, index = 0) {
    return encodeURIComponent(getNameValue(name, index));
}

function setValueByName(name, value, index = 0) {
    getItemByName(name, index).value = value;
}

function getSelectTextByName(name, index = 0) {
    return getItemByName(name, index).options[getItemByName(name, index).selectedIndex].text;
}

function setSelectItemByName(name, result, index = 0) {
    var objSelect = getItemByName(name, index);
    var list = JSON.parse(result).result;

    objSelect.innerHTML = '';
    for (var i = 0; i < list.length - 1; i++) {
        var option = document.createElement('option');

        option.text = list[i].text;
        option.value = list[i].value;
        objSelect.options.add(option);
    }
}



//Ajax访问与回调
function sendAjax(param, callback, isHandle = false) {
    var xhttp = null;
    var data = '';
    var token = '';
    var duty = '';

    if (!(param['ctrl'] == 'account' && param['func'] == 'login')) {
        token = getStorage('user_token');
        duty = getStorage('duty_token');
    }

    for (var key in param) {
        if (data == '')
            data += key + '=' + param[key];
        else
            data += '&' + key + '=' + param[key];
    }

    if (window.XMLHttpRequest)
        xhttp = new XMLHttpRequest();
    else
        xhttp = new ActiveXObject('Microsoft.XMLHTTP');

    xhttp.open('POST', 'ajax.php?' + Math.random(), true);
    xhttp.setRequestHeader('cache-control', 'no-cache');
    xhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhttp.setRequestHeader('token', token);
    xhttp.setRequestHeader('duty', duty);
    xhttp.send(data);
    xhttp.onreadystatechange = function() {
        if(xhttp.readyState == 4) {
            var result = xhttp.responseText;
            if (result != null && result != '') {
                var list = JSON.parse(result);
                if (list.lyToken != null && list.lyToken != '') {
                    setStorage('user_token', list.lyToken);
                }

                if (!isHandle) {
                    if (list.res == 1) {
                        callback(list);
                    }
                    else {
                        handleError(list);
                    }
                }
                else {
                    callback(list);
                }
            }
        }
    }
}



//常用函数
function getUrlParam(id) {
    var reg = new RegExp('(^|&)' + id + '=([^&]*)(&|$)');
    var res = window.location.search.substr(1).match(reg);
    if (res != null) {
        return unescape(res[2]);
    }
    else {
        return null;
    }
}

function toUrl(url) {
    window.location.href = url;
}

function setStorage(id, value) {
    localStorage.setItem(id, value);
}

function getStorage(id) {
    return localStorage.getItem(id);
}

function delStorage(id) {
    localStorage.removeItem(id);
}

function convertUrlParam(param) {
    var result = '';
    for (var key in param) {
        result += '&' + key + '=' + param[key];
    }
    return result;
}

function getUUID() {
    var s = [];
    var hexDigits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
    }
    var uuid = s.join('');
    return uuid;
}